package com.software.loginregister.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;

import com.software.loginregister.LoginActivity;
import com.software.loginregister.R;
import com.software.loginregister.util.SharedPreferencesUtil;

import java.io.File;

public class SettingActivity extends AppCompatActivity {

    private Button btnLogout;
    private Button btnText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        btnLogout = findViewById(R.id.btn_logout);
        initViews();
        initEvents();
    }

    private void initEvents() {
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLogoutConfirmDialog();
            }
        });
    }

    private void initViews() {
        btnLogout = findViewById(R.id.btn_logout);
    }

    private void showLogoutConfirmDialog() {
        new AlertDialog.Builder(this)
                .setTitle("退出登录")
                .setMessage("确定要退出登录吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        performLogout();
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void performLogout() {
        // 显示加载对话框
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在退出...");
        progressDialog.setCancelable(false);
        progressDialog.show();

        // 清除所有用户数据
        SharedPreferencesUtil.clearAllUserData(this);
        // 清除会话ID
        SharedPreferencesUtil.clearConversationId(this);
        // 清除口味选择状态
        SharedPreferencesUtil.setTasteSelected(this, false);

        // 清除缓存数据
        clearCache();

        // 延迟500ms后关闭对话框并跳转，让用户看到退出过程
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                progressDialog.dismiss();
                // 跳转到登录界面
                Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
                // 添加过渡动画
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }
        }, 500);
    }

    private void clearCache() {
        try {
            File cacheDir = getCacheDir();
            deleteDir(cacheDir);

            // 清除外部缓存
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                File externalCacheDir = getExternalCacheDir();
                if (externalCacheDir != null) {
                    deleteDir(externalCacheDir);
                }
            }
        } catch (Exception e) {
            Log.e("SettingActivity", "清除缓存失败", e);
        }
    }

    private boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            if (children != null) {
                for (String child : children) {
                    boolean success = deleteDir(new File(dir, child));
                    if (!success) {
                        return false;
                    }
                }
            }
        }
        return dir == null || dir.delete();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }
}